<template>
  <div>
    <el-form :model="user" ref="user" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="id">
        <el-input v-model="user.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="user.name" placeholder="请输入用户名" ref="notNull"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="user.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="选择生日" required>
        <el-col :span="11">
          <el-form-item prop="birthday">
            <el-date-picker type="date" placeholder="选择生日" v-model="user.birthday"
                            style="width: 100%;" ref="notNull"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input type="textarea" v-model="user.address" ref="notNull"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">立即修改</el-button>
        <el-button @click="resetForm()">重新填写</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "UserEdit",
  data() {
    return {
      user: {
        id: '',
        name: '',
        sex: '',
        birthday: '',
        address: '',
      },
      rules: {
        name: [
          {required: true, message: '用户名不能为空', trigger: 'blur'},
          {min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur'}
        ],
        birthday: [
          {type: 'date', required: true, message: '请选择生日', trigger: 'change'}
        ],
        address: [
          {required: true, message: '填写地址', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.user.validate((valid) => {
        if (valid) {
          //由于传过来的日期类型类似于：2021-05-11T16:00:00.000Z。
          // 直接使用对象会导致无法插入数据库，必须先进行处理成：2021-05-11的形式
          this.user.birthday = this.user.birthday.getFullYear() + '-' + (this.user.birthday.getMonth() + 1) + '-' + this.user.birthday.getDate();
          this.$http.post("http://localhost:8081/user/edit", this.user).then(res => {
            this.$router.push("/users");//将页面重定向
            this.$alert(res.data.msg, '操作结果：', {
              confirmButtonText: '确定',
            });
          });
        } else {
          this.$message({
            showClose: true,
            message: '请填写必要字段',
            type: 'warning'
          });
          return false;
        }
      });
    },
    resetForm() {
      this.user.id = this.$route.params.id;
      this.user.name = '';
      this.user.sex = this.$route.params.sex;
      this.user.birthday = '';
      this.user.address = '';
    },
  },
  created() {
    this.user.id = this.$route.params.id;
    this.user.name = this.$route.params.name;
    this.user.sex = this.$route.params.sex;
    // this.user.birthday = this.$route.params.birthday;//有问题，不再传递生日
    this.user.address = this.$route.params.address;
  },

}
</script>

<style scoped>

</style>
